package com.tledu.spring.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.spring.core.entity.AjaxResult;
import com.tledu.spring.domain.Craft;
import com.tledu.spring.domain.Role;
import com.tledu.spring.service.ICraftService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/craft/manage")
@AllArgsConstructor
public class CraftController {

    private final ICraftService craftService;

    @RequestMapping()
    public String list(Craft craft, Model model) {
        model.addAttribute("list", craftService.query(craft));
        return "craft/manage/list";
    }
    @PostMapping("list")
    @ResponseBody
    public AjaxResult<PageInfo<Craft>> listAjaxResult(Craft craft, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Craft> list = craftService.query(craft);
        PageInfo<Craft> pageInfo = new PageInfo<>(list);
        return new AjaxResult<PageInfo<Craft>>(pageInfo);
    }
    @GetMapping("/add")
    public String add() {
        return "craft/manage/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult<String> add(Craft craft) {
        craftService.insert(craft);
        System.out.println(craft);
        return new AjaxResult<>("新增成功");
    }
    @DeleteMapping("/delete/{cr_id}")
    @ResponseBody
    public AjaxResult<String> delete(@PathVariable Integer cr_id) {
        craftService.delete(cr_id);
        return new AjaxResult<>("删除成功");
    }

    @GetMapping("/edit")
    public String edit(Integer cr_id,Model model) {
        model.addAttribute("craft", craftService.selectByCr_Id(cr_id));
        return "craft/manage/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult<String> edit(Craft craft) {
        craftService.update(craft);
        return new AjaxResult<>("修改成功");
    }

}